import { showHistoryOrder } from "@/api/index";
interface IRecordItem {
  id: number;
  address: string;
  participants: string[];
  handlerName: string;
  userRemark?: string;
  status: "processed" | "rejected";
  rejectionReason?: string;
  items?: IDetailItem[];
  totalAmount: number;
  completionTime: string;
}

interface IDetailItem {
  name: string;
  unitPrice: number;
  value: number; // 出货值，可以是重量(kg)、数量(个)等
  total: number;
}

Page({
  data: {
    isLoading: true,
    recordList: [] as IRecordItem[],
  },

  onLoad() {
    this.fetchRecords();
  },

  // 模拟从服务器获取数据
  async fetchRecords() {
    this.setData({ isLoading: true });
    const data = await showHistoryOrder();
    console.log(data);
    data.forEach((element) => {
      element.endId = element._id.slice(-8);
      element.completionTime = new Date(element.updatedAt).toLocaleString();
      if (element.items) {
        element.items = element.items.map((i) => {
          return {
            name: i.categoryName,
            totalPrice: (i.totalPrice / 100).toFixed(2),
            weight: i.weight,
            unit: i.unit,
            price: (i.price / 100).toFixed(2),
          };
        });
      }
    });
    this.setData({
      recordList: data,
      isLoading: false,
    });
    // 模拟网络延迟
    // setTimeout(() => {
    //   const mockData: IRecordItem[] = [
    //     {
    //       id: 1,
    //       address: "22栋212室",
    //       participants: [
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //       ],
    //       handlerName: "张三",
    //       userRemark: "宿舍里有人，跟他们说回收的就行",
    //       status: "processed",
    //       items: [
    //         { name: "衣服", unitPrice: 0.6, value: 20, total: 12.0 },
    //         { name: "废纸", unitPrice: 0.4, value: 30, total: 12.0 },
    //         { name: "塑料瓶", unitPrice: 0.1, value: 50, total: 5.0 },
    //       ],
    //       totalAmount: 29.0,
    //       completionTime: "2023-10-26 14:30",
    //     },
    //     {
    //       id: 2,
    //       address: "18栋负一楼储藏室",
    //       participants: [
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //       ],
    //       handlerName: "李四",
    //       status: "rejected",
    //       rejectionReason:
    //         "回收物品中包含禁止回收的危险品（废旧电池），请移除后再提交。",
    //       totalAmount: 0.0,
    //       completionTime: "2023-10-25 09:15",
    //     },
    //     {
    //       id: 3,
    //       address: "3号楼大厅",
    //       participants: [
    //         "https://mp-7734cf55-8926-4ed0-a608-1302ac9a2a87.cdn.bspapp.com/avatar.png",
    //       ],
    //       handlerName: "张三",
    //       userRemark: "东西比较多，可能需要一辆小推车。",
    //       status: "processed",
    //       items: [
    //         { name: "旧家电", unitPrice: 15, value: 2, total: 30.0 },
    //         { name: "金属制品", unitPrice: 2.5, value: 10, total: 25.0 },
    //       ],
    //       totalAmount: 55.0,
    //       completionTime: "2023-10-24 18:00",
    //     },
    //   ];

    //   this.setData({
    //     recordList: mockData,
    //     isLoading: false,
    //   });
    // }, 1500); // 延迟1.5秒，模拟加载过程
  },
});
